package com.sright.bcard.cms.intercepter;

import lombok.extern.slf4j.Slf4j;
import org.jose4j.json.JsonUtil;
import org.jose4j.jwk.RsaJsonWebKey;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException;

import java.util.Map;

@Slf4j
public class TokenVerifyUtil {

    private static class RsaJsonWebKeyBuilder {

        private static volatile RsaJsonWebKey rsaJsonWebKey;

        public static RsaJsonWebKey getRasJsonWebKeyInstance() throws JoseException {
            if (rsaJsonWebKey == null) {
                synchronized (RsaJsonWebKey.class) {
                    if (rsaJsonWebKey == null) {
                        String privateKeyString = "{\"kty\":\"RSA\",\"kid\":\"81043f8c681575bca3e4eff6afbd9db9\",\"alg\":\"RS256\",\"n\":\"pOlm2cphYDUKZkTBOmhI3vkSHFe2r8IUAQCDZtjN9hWJYGRA27DWvNzRE_2IfHHnX51X-Et3nsHhdF4oVVK_G5OE-Q_s9zpJRWrGAbgmxe5wZLuIZmVQSDuXxowgpu5NUxiHV0gz9rNaZR0XjW0T3XkjOUTZI81kbrXtbjB63ffAqFkaQFg9FWpxEerj_Qizp7WN2vIyWAU8oK8pYHhtn2Bp3jZkCHg_JG--Ptk3_zKFDnnKjwjamnl0354aEhqn4eO0zjh6igpQGlEExX79Vw2dcZDNvVAImmi9-U-rt6Qbl7firzPcqEAKH85BAYoBp9xx9qWX3eD54aCHwugs4w\",\"e\":\"AQAB\",\"d\":\"d2yfEvFAe09t7Im19Po4a2CsAOJ0ZeuCWQdqITy4SpFmHuv7HB2AJN00e-mKwbQq_k8PubvNMKbvAjp9suxtgSbfkQ47aP5IuYCNTAlz6rq1of081gpgjS66xwoLS1gfnyGQQWEEYI-Z0seymrR3QlfLblmxdf_zz0joXHe3Qq5P9v2dJUT-Kcf5NvUQvP7YCHwbAQIBVn3dkwMkqqKfSqKH-XIStuCFN--sEjvv_KzpBaPAVik7vq0dRJhjyg3Jk40kZGFsawXOuXJQkZjeZyX4uLmX3Ctk6uBQKbLI4USupyR7vyvfSqSzWVyzTlydyEJjCZeiag7jdsWdV52bIQ\",\"p\":\"63KS1phhIN1sy0FBnSWsHbEoahF5b-S_6shmhUIUKmsHxG9NlPw1Ae6ziCvhG4bHS_JF7WEeCRE7fgK0mH8zKAJZUApbZS8A9QrUHx1U1GrkvJQuU15z3mS2qY6T4S7NWnqsPHOItEUQXHmh6SXYf6Qm5qUaEgbhN2Y6IN5VWR8\",\"q\":\"s06ZmeFNjAhRWMBLVhAWXynbxAbDjRcJk0I9bFtIe9W5s-63CC0bBVNPqXlkBc_tMCn2xYAG_I8ru5sYoE56ZC8TrrKzam142fQPIPhP3_PAn6A0PMLpiUL61uSJPwB4QGnGT1OZNvsadVMP2_Ovt14MFu_2sOsJRDtFJxBxf70\",\"dp\":\"bZWzIGxl8Kz-oQ-rYWASXvOA0vV1Jw46ASXrCaT6y89rGxOO8YP9i_u01fFOYch2ZDPMNjMYjrHVlc3ejofNoJaPSUC4zzx0Xpj-p8i4HfHuqpLxiN4SYfVQS1TQIWPuMmmcVrqTawwmFFSMdn6l6KrXbEx1-OP42RccgQXahZM\",\"dq\":\"DuW3404RyFGkmJAZckQR58QtiyLYum8wj3iQa-FFq2w9apKpyFcfCJk8ZCM-yTDWTwsdgpk5e2BVzNd8-0yp5Ir4wqn8OBpyUQFzBFU9Jak11s1Tqbyz9ICJILOKJ57dXtSq8kpKz4ObViYZije74KtCq-EyCM7uTSnze-Rk2a0\",\"qi\":\"G_O8Hz5NDyzZ_7210wgQwyVQY850txVU0zXzAoCiDc3njcylwOQ82gvZ-dfsiktphlzFsyEei0ZoLdKgcpwknov1hBhA8pn78SppVW6CWxvye1REvz-agt6RrhAmHwDGf6wbcYwaDr8j9bYZH48MUCgGEw1ja0PNpE9Dp7_Egko\"}";
                        rsaJsonWebKey = new RsaJsonWebKey(JsonUtil.parseJson(privateKeyString));
                    }
                }
            }
            return rsaJsonWebKey;
        }
    }

    public static Map<String, Object> verifyToken(String token) throws JoseException, InvalidJwtException {
        String tokenContent = token;
        if (token.startsWith("Bearer")) {
            tokenContent = token.substring(7);
        }

        JwtConsumer consumer = new JwtConsumerBuilder()
                .setRequireExpirationTime() //JWT must have an expiration time
                .setAllowedClockSkewInSeconds(30) //allow some leeway in validating time based claims to account for clock skew
                .setExpectedIssuer("ergeng")
                .setVerificationKey(RsaJsonWebKeyBuilder.getRasJsonWebKeyInstance().getKey())
                .build();
        JwtClaims claims = consumer.processToClaims(tokenContent);
        return claims.getClaimsMap();
    }
}
